#include "video/window.h"

namespace video {
	Window::Window(const std::string& name):			
		util::Named(name),
		mTitle(name)
	{
		mWidth = 1024;
		mHeight = 786;

		mRedBits = 8;
		mGreenBits = 8;
		mBlueBits = 8;
		mAlphaBits = 8;

		mDepthBits = 24;
		mStencilBits = 8;

		mFullscreen = false;
		mAutoSizeWindow = false;
		
		mTitle = "MainWindow";

		mScreenAspectRatio = 1.0;
		mScreenAspectType = UNKNOWN_SCREEN;
	}

	Window::~Window() {
	}

	Window::eScreenAspect Window::getScreenAspectType() const {
		return mScreenAspectType;
	}

	float Window::getScreenAspectRatio() const {
		return mScreenAspectRatio;
	}

	void Window::create() {
		glfwInit();

		glfwGetDesktopMode(&mDesktopMode);

		mScreenAspectRatio = static_cast<float>(mDesktopMode.Width) / static_cast<float>(mDesktopMode.Height);
		
		switch (static_cast<int>(mScreenAspectRatio * 100)) {
		case 160:
			mScreenAspectType = Window::WIDE_SCREEN;
			break;
		case 133:
		case 125:		
			mScreenAspectType = Window::NORMAL_SCREEN;
			break;
		default:
			mScreenAspectType = Window::UNKNOWN_SCREEN;
		}

		if (mAutoSizeWindow) {
			mWidth = mDesktopMode.Width;
			mHeight = mDesktopMode.Height;

			mRedBits = mDesktopMode.RedBits;
			mGreenBits = mDesktopMode.GreenBits;
			mBlueBits = mDesktopMode.BlueBits;
		}

		glfwOpenWindow(
			mWidth, 
			mHeight, 

			mRedBits, 
			mGreenBits, 
			mBlueBits, 
			mAlphaBits, 
			
			mDepthBits, 
			mStencilBits, 

			mFullscreen?  GLFW_FULLSCREEN : GLFW_WINDOW 
		);

		glfwSetWindowTitle(getName().c_str());

		if (mFullscreen)
			glfwDisable(GLFW_MOUSE_CURSOR);

		glfwSwapInterval(0); //disable vertical synchronization
	}

	void Window::destroy() {
		if (mFullscreen)
			glfwEnable(GLFW_MOUSE_CURSOR);

		glfwCloseWindow();
		glfwTerminate();
	}
}
